import { Refund } from 'refund-model.js';
var refund = new Refund();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenSelBox:false,
    radioItems: [
      { name: '质量问题', value: '质量问题' },
      { name: '卖家发错货', value: '卖家发错货', checked: 'true' },
      { name: '七天无理由', value: '七天无理由' },
      { name: '商品与页面图片不符', value: '商品与页面图片不符' },
      { name: '发票问题', value: '发票问题' },
      { name: '其它', value: '其它' },
    ],
    reasonIndex:1,
    reasonText: '',
    method:[
      { name: '原支付返还',icon:'icon-tuihuo1',value:0,checked:true},
      { name: '账户余额', icon: 'icon-weibiaoti2fuzhi04',value:1}
    ],
    selectMethod:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.sku_id = options.sku_id ? options.sku_id : '';
    this.data.type = options.type ? options.type : ''; //1换货，2退货退款
    this.data.order_id = options.order_id ? options.order_id : '';
    if(this.data.type == 1){
      wx.setNavigationBarTitle({
        title: '换货',
      }) 
    } else if (this.data.type ==2){
      wx.setNavigationBarTitle({
        title: '退货退款',
      }) 
    }
    this.setData({
      type : this.data.type
    })
    if (!this.data.sku_id || !this.data.type || !this.data.order_id) {
      return false
    }
    this._loadData();
  },
  _loadData: function () {
    var that = this;
    var args = {
      id: this.data.sku_id,
      order_id: this.data.order_id
    }
    refund.getSkuInfo(args, (res) => {
      var data = res.item;
      that.setData({
        info: data,
        name: data.order.full_name,
        mobile: data.order.tel
      })
      
    });
  },
  radioChange: function (e) {
    var radioItems = this.data.radioItems;
    for (var i = 0, len = radioItems.length; i < len; ++i) {
      
      if(radioItems[i].value == e.detail.value){
        radioItems[i].checked = true;
        this.data.reasonIndex = i;
      }else{
        radioItems[i].checked = false;
      }
    }
    this.setData({
      radioItems: radioItems
    });
  },
  selectMethod: function (e) {
    var index = e.currentTarget.dataset.index;
    var method = this.data.method;
    for (var i = 0, len = method.length; i < len; ++i) {
      if (i == index) {
        method[i].checked = true;
      } else {
        method[i].checked = false;
      }
    }
    this.setData({
      method,
      selectMethod:index
    });
  },
  showSelectBox:function(){
    this.setData({
      hiddenSelBox: false,
    })
  },
  hideSelectBox: function () {
    var index = this.data.reasonIndex;
    var text = this.data.radioItems[index].value
    this.setData({
      hiddenSelBox:true,
      reasonText : text
    })
  },
  //获取联系人输入内容
  name: function (e) {
    var name = e.detail.value;
    this.data.name = name
  },
  //获取手机号码输入内容
  mobile:function(e){
    var mobile = e.detail.value;
    this.data.mobile = mobile
  },
  sub:function(){
    var index = this.data.reasonIndex;
    var text = this.data.radioItems[index].value
    if(!this.data.mobile){
      wx.showToast({
        title: '请填写联系电话',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    
    if(this.data.type == 1){
      //换货
      var args = {
        order_sku_id: this.data.sku_id,
        note: text,
        type: 2,
        name: this.data.name,
        mobile: this.data.mobile
      };
      refund.applyExchange(args, (res) => {
        if (res.status == 1) {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000,
            success:function(){
              setTimeout(function () {
                wx.redirectTo({
                  url: 'refund?index=1',
                })
              }, 2000)
            }
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      });
    }
    else if (this.data.type == 2){
      var args = {
        order_sku_id: this.data.sku_id,
        note: text,
        refund_type: this.data.selectMethod,
        name: this.data.name,
        mobile: this.data.mobile
      };
      refund.applyService(args, (res) => {
        if(res.status==1){
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000,
            success: function () {
              setTimeout(function () {
                wx.redirectTo({
                  url: 'refund?index=1',
                })
              }, 2000)
            }
          })
        }else{
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      });
    }
  }
})